package com.atguigu.eduservice.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.atguigu.baseservice.handler.exception.GuliException;
import com.atguigu.baseservice.result.ResultCodeEnum;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.ExcelSubject;
import com.atguigu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.Map;

/**
 * ExcelSubject的Listener监听类
 */
public class SubjectExcelListener extends AnalysisEventListener<ExcelSubject> {

    public EduSubjectService subjectService;

    //空参构造
    public SubjectExcelListener() {
    }

    //有参构造，传递EduSubjectService用于操作数据库
    public SubjectExcelListener(EduSubjectService subjectService) {
        this.subjectService = subjectService;
    }

    /**
     * 读取excel表头信息
     * @param headMap
     * @param context
     */
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        System.out.println("表头信息：" + headMap);
    }

    /**
     * 一行一行的读取excel
     * @param excelSubject
     * @param analysisContext
     */
    @Override
    public void invoke(ExcelSubject excelSubject, AnalysisContext analysisContext) {
        if (excelSubject == null){
            throw new GuliException(ResultCodeEnum.FILE_UPLOAD_ERROR);
        }

        //添加一级分类
        EduSubject eduOneSubject = this.existOneSubject(subjectService, excelSubject.getOneSubjectName());
        if (eduOneSubject == null){
            //如果不存在一级分类，则新增
            eduOneSubject = new EduSubject();
            eduOneSubject.setTitle(excelSubject.getOneSubjectName());
            eduOneSubject.setParentId("0");
            subjectService.save(eduOneSubject);
        }

        //获取一级分类的id值
        String pid = eduOneSubject.getId();

        //添加二级分类
        EduSubject eduTwoSubject = this.existTwoSubject(subjectService, excelSubject.getTwoSubjectName(), pid);
        if (eduTwoSubject == null){
            //如果不存在二级分类，则新增
            eduTwoSubject = new EduSubject();
            eduTwoSubject.setTitle(excelSubject.getTwoSubjectName());
            eduTwoSubject.setParentId(pid);
            subjectService.save(eduTwoSubject);
        }

    }

    /**
     * 读取完成后执行
     * @param analysisContext
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        System.out.println("导入完成");
    }

    /**
     * 判断一级分类是否重复（从数据库判断）
     * @param subjectService
     * @param name
     * @return
     */
    private EduSubject existOneSubject(EduSubjectService subjectService, String name){
        //创建查询条件
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title", name).eq("parent_id", "0");
        //获取符合条件的数据并返回
        return subjectService.getOne(wrapper);
    }

    /**
     * 判断二级分类是否重复（从数据库判断）
     * @param subjectService
     * @param name
     * @param pid
     * @return
     */
    private EduSubject existTwoSubject(EduSubjectService subjectService, String name, String pid){
        //创建查询条件
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title", name).eq("parent_id", pid);
        return subjectService.getOne(wrapper);
    }


}
